# -*- coding: utf-8 -*-
"""
@Time    : 2024/6/27 13:32 
@Author  : ZhangShenao 
@File    : type_convert.py 
@Desc    : 类型转换相关的内置函数
"""

# dict()函数:将参数转换为dict类型
d = dict(a=1, b=2, c=3)
# type if d is: <class 'dict'>, value of d is: {'a': 1, 'b': 2, 'c': 3}
print(f'type if d is: {type(d)}, value of d is: {d}')

# float()函数:将字符串或数字转换为浮点型
f = float('3.14')
# type if f is: <class 'float'>, value of f is: 3.14
print(f'type if f is: {type(f)}, value of f is: {f}')

# int()函数:将字符串或数字转换为整数
i1 = int('123')
# type if i1 is: <class 'int'>, value of i1 is: 123
print(f'type if i1 is: {type(i1)}, value of i1 is: {i1}')
i2 = int(3.14)
# type if i2 is: <class 'int'>, value of i2 is: 3
# 将float转换为int时,小数部分会自动进行截断
print(f'type if i2 is: {type(i2)}, value of i2 is: {i2}')

# list()函数: 将tuple、str等可迭代对象转换为list
l1 = list('abc')
# type if l1 is: <class 'list'>, value of l1 is: ['a', 'b', 'c']
print(f'type if l1 is: {type(l1)}, value of l1 is: {l1}')
l2 = list((1, 2, 3))
# type if l2 is: <class 'list'>, value of l2 is: [1, 2, 3]
print(f'type if l2 is: {type(l2)}, value of l2 is: {l2}')

# tuple()函数:将list、str等可迭代对象转换为tuple
t1 = tuple('abc')
# type if t1 is: <class 'tuple'>, value of t1 is: ('a', 'b', 'c')
print(f'type if t1 is: {type(t1)}, value of t1 is: {t1}')
t2 = tuple([1, 2, 3])
# type if t2 is: <class 'tuple'>, value of t2 is: (1, 2, 3)
print(f'type if t2 is: {type(t2)}, value of t2 is: {t2}')

# set()函数: 1.创建空set；2.将可迭代对象转换为set
# 将可迭代对象转换为set时,会自动进行去重操作
s1 = set()  # 创建空set
# type if s1 is: <class 'set'>, value of s1 is: set()
print(f'type if s1 is: {type(s1)}, value of s1 is: {s1}')
s2 = set('aaabbbc')
# type if s2 is: <class 'set'>, value of s2 is: {'c', 'a', 'b'}
print(f'type if s2 is: {type(s2)}, value of s2 is: {s2}')
s3 = set([1, 1, 1, 2, 2, 2, 3, 3])
# type if s3 is: <class 'set'>, value of s3 is: {1, 2, 3}
print(f'type if s3 is: {type(s3)}, value of s3 is: {s3}')

# str()函数: 将其他类型转换为str
s1 = str(111)
print(f'type if s1 is: {type(s1)}, value of s1 is: {s1}')
s2 = str(3.14)
print(f'type if s2 is: {type(s2)}, value of s2 is: {s2}')

# bytes()函数: 将参数转换为字节序列
b1 = bytes('abc', encoding='utf-8')
# type if b1 is: <class 'bytes'>, value of b1 is: b'abc'
print(f'type if b1 is: {type(b1)}, value of b1 is: {b1}')
b2 = bytes(4)
# type if b2 is: <class 'bytes'>, value of b2 is: b'\x00\x00\x00\x00'
print(f'type if b2 is: {type(b2)}, value of b2 is: {b2}')
